package com.wn.search.service.impl;
import com.google.common.collect.Lists;

import com.wn.sddfp.common.model.dto.borrow.Pages;
import com.wn.search.model.BorrowRecordIndexVo;
import com.wn.search.model.BorrowRecordSearchQuery;
import com.wn.search.service.SearchService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yellow Docter
 * @date 2024-08-08
 * @desc 蜗牛创想：www.woniuxy.com
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public Pages<BorrowRecordIndexVo> search(BorrowRecordSearchQuery borrowRecordSearchQuery) {

        //创建本地构造器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        // 构建查询条件
        if (StringUtils.hasText(borrowRecordSearchQuery.getName())) {
            //通过name 字段 进行查询
            queryBuilder.withQuery(QueryBuilders.matchQuery("name", borrowRecordSearchQuery.getName()));
        }

        //通过 period 进行 范围查询
        if (borrowRecordSearchQuery.getStartPeriod() != null && borrowRecordSearchQuery.getEndPeriod() != null) {
            queryBuilder.withQuery(QueryBuilders.rangeQuery("period")
                    .from(borrowRecordSearchQuery.getStartPeriod())
                    .to(borrowRecordSearchQuery.getEndPeriod()));
        }

        // 构建分页
        int pageNo = borrowRecordSearchQuery.getPageNo() != null ? borrowRecordSearchQuery.getPageNo() : 0;
        int pageSize = borrowRecordSearchQuery.getPageSize() != null ? borrowRecordSearchQuery.getPageSize() : 10;
        queryBuilder.withPageable(PageRequest.of(pageNo, pageSize));

        //创建 搜索条件
        NativeSearchQuery searchQuery = queryBuilder.build();

        // 执行查询
        SearchHits<BorrowRecordIndexVo> searchHits = elasticsearchRestTemplate.search(searchQuery, BorrowRecordIndexVo.class);

        // 转换结果为分页对象  hit 转成 BorrowRecordIndexVo
        List<BorrowRecordIndexVo> results = searchHits
                .stream()
                .map(SearchHit::getContent)
                .collect(Collectors.toList());

        //拿到总数
        long totalHits = searchHits.getTotalHits();

        //返回 page分页数据
        Pages<BorrowRecordIndexVo> pages = new Pages();
        pages.setList(results);
        pages.setTotal(totalHits);
        pages.setPageNum(borrowRecordSearchQuery.getPageNo());
        pages.setPageSize(borrowRecordSearchQuery.getPageSize());
        pages.setTotal(totalHits);

        return pages;
    }

}
